package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.joplus.housekeeper.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ccm on 2016/11/19 14:58
 */
public class EvaluateSuggestAdapter extends BaseAdapter {
    private Context context;

    private LinearLayout ll;

    String[] s;

    List<String> selectEdSuggest = new ArrayList<>();

    private onSelectedSuggestListener onSelectedSuggest;

    Boolean selectAble = true;

    public EvaluateSuggestAdapter(Context context) {
        this.context = context;
    }

    public void setOnSelectedSuggest(onSelectedSuggestListener onSelectedSuggest) {
        this.onSelectedSuggest = onSelectedSuggest;
    }

    public void setData(String[] tips) {
        s = tips;
        notifyDataSetChanged();
        selectEdSuggest.clear();
    }

    public void setSelectEdSuggest(List<String> selectEdSuggest) {
        this.selectEdSuggest = selectEdSuggest;
        selectAble = false;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (s == null) {
            return 0;
        } else {
            return s.length;
        }

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_evaluate_suggest, null);
        final TextView tv = (TextView) convertView.findViewById(R.id.tv_suggest);
        tv.setText(s[position]);
        if (selectAble) {
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!selectEdSuggest.contains(s[position])) {
                        selectEdSuggest.add(s[position]);
                        tv.setTextColor(Color.parseColor("#ffffff"));
                        tv.setBackgroundResource(R.drawable.shape_gray);
                    } else {
                        selectEdSuggest.remove(s[position]);
                        tv.setTextColor(Color.parseColor("#000000"));
                        tv.setBackgroundResource(R.drawable.shape_gray_coner);
                    }

                    if (onSelectedSuggest != null) {
                        onSelectedSuggest.onSelected(selectEdSuggest);
                    }
                }
            });
        } else {
            if (selectEdSuggest.contains(s[position])) {
                tv.setTextColor(Color.parseColor("#ffffff"));
                tv.setBackgroundResource(R.drawable.shape_gray);
            }
        }
        return convertView;
    }

    public interface onSelectedSuggestListener {
        void onSelected(List<String> s);
    }

}
